﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.ServiceModel;
using COFE.Core;
using COFE.Core.Profile;
using System.Web;

namespace COFE.Web
{
    /// <summary>
    /// WCF related tools.
    /// </summary>
    public static class WSUtils
    {
        /// <summary>
        /// Gets the host of incoming request.
        /// </summary>
        public static string GetHost()
        {
            if (OperationContext.Current == null)
                return "";

            Uri uri = OperationContext.Current.IncomingMessageHeaders.To;
            if (uri.Port == 80)
                return String.Format("{0}://{1}", uri.Scheme, uri.Host);
            else return String.Format("{0}://{1}:{2}", uri.Scheme, uri.Host, uri.Port);
        }

        //public static bool LoginCurrentUser()
        //{
        //    try
        //    {
        //        if (String.IsNullOrEmpty(System.Web.HttpContext.Current.User.Identity.Name))
        //            return false;

        //        COFEWSShared.UserProfileManager
        //            .Login(System.Web.HttpContext.Current.User.Identity.Name);
        //        return true;
        //    }
        //    catch { return false; }            
        //}

        public static bool LoginUsingCookie()
        {
            var sig = HttpContext.Current.Request.Cookies["cofews_signature"];
            if (sig == null)
                return false;
            try
            {
                var user = COFEWSShared.UserProfileManager.Login(new Guid(sig.Value));
                return user != null;
            }
            catch { return false; }

        }

        public static bool Login(string userName, string password)
        {
            try
            {
                COFEWSShared.UserProfileManager.Login(userName, password);
                return true;
            }
            catch { return false; }
        }
    }

}
